{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数定义\n",
    "# python 中函数不支持重载，后面的会覆盖前面的\n",
    "def foo():\n",
    "    print(\"foo\")\n",
    "    print(\"is a function\")\n",
    "    return \"ok\"\n",
    "\n",
    "def foo(name):\n",
    "    return name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "foo\n",
      "is a function\n"
     ]
    }
   ],
   "source": [
    "# 函数调用\n",
    "var = foo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# lambda表达式 定义匿名函数\n",
    "bar = lambda x: x + 1\n",
    "foo = lambda x, y: (print(y), x / y) if y != 0 else (print(y), x if x != 0 else 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "100\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "500"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 类型提示，非类型约束，解释型动态类型语言:\n",
    "# 参数类型提示，不指定为任何类型 Any\n",
    "# 返回值类型提示\n",
    "\n",
    "# 参数：固定参数，默认值参数，\n",
    "\n",
    "# 指定参数默认值,不传入为默认值\n",
    "\n",
    "def foo2(number,n2:int=1,n3:int=3) -> int:\n",
    "    print(number)\n",
    "    return n2 + n3 \n",
    "\n",
    "n1 = 100\n",
    "n2 = 200\n",
    "n3 = 300\n",
    "\n",
    "foo2(n1,n2,n3)\n",
    "\n",
    "# 关键字参数传入\n",
    "foo2(number=n1,n3=n3,n2=n2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 不定长参数，可以为空，非关键字\n",
    "# * 接收位置参数  元组方式存储\n",
    "# ** 接收关键字参数  字典方式存储\n",
    "def address_book(name, *telphone, alias_name=None, **custom):\n",
    "    print(f\"name: {name}, tel: {telphone}, aname: {alias_name}, custom:{custom}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "address_book(\"wilson\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "address_book(\"wilson\",1234,4567,5678)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "address_book(\"wilson\",1234,4567,5678,home=\"Guangdong\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "address_book(\"wilson\",1234,4567,5678,alias_name='w',aaa=\"bbb\", home=\"Guangdong\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 编写函数文档\n",
    "def foo():\n",
    "    \"\"\" 演示 \"\"\"\n",
    "    print(\"test\")\n",
    "\n",
    "# 查看函数文档\n",
    "foo.__doc__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# dir 内省：查看对象内置属性与方法\n",
    "list1 = []\n",
    "dir(list1)\n",
    "# 查看函数内省\n",
    "dir(foo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 高阶函数 map => C# Select用法\n",
    "# 将可迭代对象元素作为参数，逐一传入执行\n",
    "def add(number):\n",
    "    return ( number + number )\n",
    "\n",
    "# range(5)  0 1 2 3 4\n",
    "# map(add, range(5) 0 2 4 6 7\n",
    "for i in map(add, range(5) ):\n",
    "    print( i )\n",
    "\n",
    "# 简化\n",
    "print(list(map(lambda x: x+x, range(5))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2]\n"
     ]
    }
   ],
   "source": [
    "# 高阶函数 filter => C# Where用法\n",
    "# 过滤可迭代对象\n",
    "print(list(filter(lambda x: x>0,(-2,-1,1,2))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 高阶函数 reduce\n",
    "# 可迭代对象元素累加\n",
    "from functools import reduce\n",
    "reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 偏函数，固定化函数参数，方便都次引用\n",
    "from functools import partial\n",
    "\n",
    "int_16 = partial(int, base=16)\n",
    "int_16(\"0f\") # int(\"0f\", base=16)\n",
    "\n",
    "wopen = partial(open,mode=\"w\",encoding=\"UTF-8\")\n",
    "wopen(\"a.txt\")  # open(\"a.txt\",mode=\"w\",encoding=\"UTF-8\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 递归\n",
    "# 阶乘运算\n",
    "def func(n:int) -> int:\n",
    "    if n == 1:\n",
    "        return n\n",
    "    return n * func(n-1)\n",
    "\n",
    "print(func(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数式编程\n",
    "def func_out():\n",
    "    var_out = 1 # 闭包\n",
    "    def func_in():\n",
    "        return var_out\n",
    "    return func_in\n",
    "\n",
    "print(func_out()())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数 - 装饰器语法\n",
    "import time\n",
    "\n",
    "# import functools\n",
    "# functools.wraps()\n",
    "from functools import wraps\n",
    "\n",
    "def time_it(func):\n",
    "    @wraps(func) # wrok装饰以后，防止原始函数被替换 work.__name__\n",
    "    def wrapper():\n",
    "        start = time.time()\n",
    "        func()\n",
    "        stop = time.time()  \n",
    "        print(f\"func 函数一共执行了{int(stop-start)}秒\")\n",
    "    return wrapper\n",
    "\n",
    "@time_it\n",
    "def work():\n",
    "    print(\"func函数开始执行\")\n",
    "    time.sleep(1)\n",
    "    print(\"func函数执行结束\")\n",
    "\n",
    "work()  # 直接调用work()\n",
    "work.__name__"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
